#include "DeclareFile.h"

#include "tools.h"

void ShowRank() {
	int index_max = num_ranking  / 40 + 1; // 总页数
	int rank_num = 40 * (PersonIndex - 1) + 1; // 排名
	setbkcolor(WHITE);
	BeginBatchDraw();
	cleardevice();

	putimage(0, 0, &TitleImg);
	setbkmode(TRANSPARENT);

	COLORREF color1 = WHITE;
	COLORREF color2 = RGB(200, 200, 200);

	setlinecolor(BLACK);
	setlinestyle(PS_SOLID, 2);
	settextcolor(BLACK);
	settextstyle(16, 0, "宋体", 0, 0, 800, 0, 0, 0);

	char str1[] = "排行榜";
	int tw = textwidth(str1); // 文本宽度
	int th = textheight(str1); // 文本高度
	int tx = (getwidth() - tw) / 2; // 文本坐标x
	int ty = 20; // 文本坐标y
	outtextxy(tx, ty, str1);

	int rx = 20;
	int ry = 60;
	rectangle(rx, ry, 780, 445);

	// 表格横线
	int h = 35; // 表格高度
	for (int i = 1; i <= 11; i++) {
		line(rx, ry + i * h, 780, ry + i * h);
	}

	// 表格竖线
	int w = 95;
	for (int i = 1; i <= 7; i++) {
		line(rx + i * w, ry, rx + i * w, 445);
	}

	// 表格头
	char str2[] = "排名";
	char str3[] = "分数";
	int textX = rx + w / 2 - textwidth(str2) / 2;
	int textY = ry + h / 2 - textheight(str2) / 2;
	for (int i = 0; i < 8; i++) {
		if (i % 2 == 0) {
			outtextxy(textX + w * i, textY, str2);
			// 绘制排名数字
			for (int j = 0; j < 10; j++) {
				char str_num[10] = "";
				sprintf_s(str_num, "%d", rank_num);
				int numX = rx + w / 2 - textwidth(str_num) / 2;
				int numY = h + ry + h / 2 - textheight(str_num) / 2;
				outtextxy(numX + w * i, numY + h * j, str_num);
				rank_num++;
			}
		}
		else {
			outtextxy(textX + w * i, textY, str3);
			// 绘制分数
			for (int j = 0; j < 10; j++) {
				char str_score[10] = "";
				sprintf_s(str_score, "%d", ranking[rank_num - 1]);
				int numX = rx + w / 2 - textwidth(str_score) / 2;
				int numY = h + ry + h / 2 - textheight(str_score) / 2;
				outtextxy(numX + w * i, numY + h * j, str_score);
			}
		}
	}

	// 返回菜单按钮
	char str4[] = "返回菜单";
	int bx1 = rx + w / 2;
	int by = 520;
	if (button(bx1, by, str4, 10, color1, color2)) {
		flag = 1;
		setbkcolor(BLACK);
		cleardevice();
		return;
	}

	// 页数统计文字

	char str5[20] = "";
	sprintf_s(str5, "共%d页，第%d页", index_max, PersonIndex);
	int index_x = 250;
	int index_y = 520 - textheight(str5) / 2;
	outtextxy(index_x, index_y, str5);

	// 上一页按钮
	char str6[] = "上一页";
	int bx2 = 526;
	if (button(bx2, by, str6, 10, color1, color2)) {
		if (PersonIndex > 1) {
			PersonIndex--;
		}
	}

	// 下一页按钮
	char str7[] = "下一页";
	int bx3 = 599;
	if (button(bx3, by, str7, 10, color1, color2)) {
		if (PersonIndex < index_max) {
			PersonIndex++;
		}
	}

	// 第一页按钮
	char str8[] = "第一页";
	int bx4 = 672;
	if (button(bx4, by, str8, 10, color1, color2)) {
		PersonIndex = 1;
	}

	// 结尾页按钮
	char str9[] = "结尾页";
	int bx5 = 745;
	if (button(bx5, by, str9, 10, color1, color2)) {
		PersonIndex = index_max;
	}

	ms.message = 0;
	EndBatchDraw();
}